#include <glad/glad.h>
#include <GLFW/glfw3.h>
#include <iostream>
#define STB_IMAGE_IMPLEMENTATION
using namespace std;

void framebuffer_size_callback(GLFWwindow* window, int width, int height);
void processInput(GLFWwindow *window);

// window size


#include "Camera.h"
#include "Mouse.h"
#include "Chunk/ChunkManager.h"
#include "noise/PerlinNoise.hpp"
#include "baseClass/GLog.h"
#include "Test.h"
#include "MCWorld.h"




int main()
{
    
    // glfw: initialize and configure
    glfwInit();
    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); // OpenGL 3.3
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3); // OpenGL 3.3
    glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);

    // glfw window creation
    GLFWwindow* window = glfwCreateWindow(MCWorld::GetSCR_WIDTH(), MCWorld::GetSCR_HEIGHT(), "LearnOpenGL", NULL, NULL);
    if (window == NULL)
    {
        LOG("Failed to create GLFW window");
        glfwTerminate();
        return -1;
    }
    glfwMakeContextCurrent(window);
    glfwSetFramebufferSizeCallback(window, framebuffer_size_callback); //

    // glad: load all OpenGL function pointers
    // ---------------------------------------
    if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
    {
        LOG("Failed to initialize GLAD");
        return -1;
    }    

    glEnable(GL_DEPTH_TEST);
    // glEnable(GL_CULL_FACE); //启用面剔除
    //隐藏光标
    glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
    BlockBase::LoadBlocksTexture();


    Test();

    Blockshader Bshader(MCWorld::GetCamera().GetProjectionMatrix());



    ChunkManager chunkManager;
  
    Mouse mouse(window);
    MCWorld::GetCamera().SetPos(0.0f, 0.0f, 70.0f);

    
// use a simple shader (assuming it's already compiled and bound)

   
    // render loop
    // -----------
    while (!glfwWindowShouldClose(window))
    {
        // input
        // -----
        processInput(window);
        mouse.updata();
        MCWorld::GetCamera().ProcessMouseMovement(mouse.GetOffsetX(),mouse.Mouse::GetOffsetY());

        chunkManager.GenerateChunks(glm::vec2(MCWorld::GetCamera().position.x,MCWorld::GetCamera().position.y));
        // render
        // ------
        glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
        glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);

        
        chunkManager.RenderChunks(Bshader,MCWorld::GetCamera());
        // glfw: swap buffers and poll IO events (keys pressed/released, mouse moved etc.)
        // -------------------------------------------------------------------------------
        glfwSwapBuffers(window);
        glfwPollEvents();
    }
    // glfw: terminate, clearing all previously allocated GLFW resources.
    // ------------------------------------------------------------------
    glfwTerminate();
    return 0;
}

// process all input: query GLFW whether relevant keys are pressed/released this frame and react accordingly
// ---------------------------------------------------------------------------------------------------------
void processInput(GLFWwindow *window)
{
    if(glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
        glfwSetWindowShouldClose(window, true);
    else if(glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS)
        MCWorld::GetCamera().ProcessKeyboard(Camera_Movement::FORWARD, 0.01f);
    else if(glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS)
        MCWorld::GetCamera().ProcessKeyboard(Camera_Movement::BACKWARD, 0.01f);
    else if(glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS)
        MCWorld::GetCamera().ProcessKeyboard(Camera_Movement::LEFT, 0.01f);
    else if(glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS)
        MCWorld::GetCamera().ProcessKeyboard(Camera_Movement::RIGHT, 0.01f);

 
}

// glfw: whenever the window size changed (by OS or user resize) this callback function executes
// ---------------------------------------------------------------------------------------------
void framebuffer_size_callback(GLFWwindow* window, int width, int height)
{
    // make sure the viewport matches the new window dimensions; note that width and 
    // height will be significantly larger than specified on retina displays.
    glViewport(0, 0, width, height);
}